//
//  DetailMyBoardUIViewController.m
//  Bespoke
//
//  Created by Cheng on 12/7/23.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DetailMyBoardUIViewController.h"
#import "HomePageViewController.h"
#import "DetailMyBoardCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation DetailMyBoardUIViewController
@synthesize Array_MediaSelected, SelectedBoardID, AB_DetailMyboard;
@synthesize SVw_Scroll, IVw_dottedline;
@synthesize Bt_FullScreenOff,Lab_title,Bt_Media,Lab_BoardName,Bt_EditClippings;
@synthesize Array_AccountInfo, IVw_AccountPhoto, Vw_QuestionView;
@synthesize Vw_EditClippings, Vw_CreateBook, Vw_PhotoList, Vw_SelectClipping, IVw_Background;
@synthesize BK_En_object;
@synthesize delegate;

bool edit = FALSE;

#define BoarderHight 100
#define SCREENWIDTH 1024
#define SCREENHEIGHT 768
#define SCROLLHEIGHT 606
#define SCROLLVIEW_AXIS_Y 100
#define SCROLLVIEW_FULLSCREEN_AXIS_Y 70
#define SHARE_ARROW_HEIGHT 10

#define MEDIA_TAG 200000
#define M_BUFFER_X 20
#define M_BUFFER_Y 20
#define FULLSCREEN_BACKGROUND_TAG 500000

#define PHOTO_ARROW_AXIS_X 37
#define QUESTION_ARROW_AXIS_X 492

#define ModifyDetailMyBoard_API @"http://54.235.166.43/BeSpokeAPI/MyBoard/ModifyDetailMyBoard.php"

int m_Max_Y = 0;

- (void) fetchData {
    
    int MediaIndex = 0;
    
    MediaListsMedia *MLM_Media = [[MediaListsMedia alloc] init];
    
    //Init x and y of all media.
    for (MLM_Media in self.AB_DetailMyboard.MediaArray)
        MLM_Media.x=MLM_Media.y=0;
    
    for (MLM_Media in self.AB_DetailMyboard.MediaArray) {
        
        DetailMyBoardCell *Vw_DetailMyBoardCell = [[[NSBundle mainBundle] loadNibNamed:@"DetailMyBoardCell" owner:self options:nil] objectAtIndex: 0];
        
        Vw_DetailMyBoardCell.tag = MediaIndex+MEDIA_TAG;
        [Bt_Media addTarget:self action:@selector(MediaPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        if(self.interfaceOrientation == UIInterfaceOrientationPortrait)
            [Vw_DetailMyBoardCell updateDetailCellInfo:MLM_Media
                                   MaxWidth:self.view.frame.size.width
                                 FullScreen:FALSE];
        else 
            [Vw_DetailMyBoardCell updateDetailCellInfo:MLM_Media
                                   MaxWidth:self.view.frame.size.height
                                 FullScreen:FALSE];
                
        //Dynamically Set the XY of the Media Location
        [self SetMediaLocation:MLM_Media index:MediaIndex];
        
        Vw_DetailMyBoardCell.frame = CGRectMake(MLM_Media.x, MLM_Media.y, MLM_Media.Width, MLM_Media.Height);
        
        [SVw_Scroll addSubview:Vw_DetailMyBoardCell];
        [Array_MediaSelected addObject:@"NonSelect"];
        
        if((MLM_Media.y + MLM_Media.Height + M_BUFFER_Y)> SVw_Scroll.contentSize.height)
            SVw_Scroll.contentSize = CGSizeMake(SCREENWIDTH , MLM_Media.y + MLM_Media.Height+ M_BUFFER_Y);
        
        MediaIndex++;
    }
    //NSLog(@"After media select status:%@",Array_MediaSelected);

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.IVw_AccountPhoto setImage:self.BK_En_object.yourPhoto];
    self.Array_MediaSelected = [[NSMutableArray alloc] init];
    
    m_Max_Y = 0;
    edit = FALSE;
    
    Lab_BoardName.text = [NSString stringWithFormat:@"{ %@ }",AB_DetailMyboard.name];
    
    if(self.interfaceOrientation == UIInterfaceOrientationPortrait)
        self.SVw_Scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SCROLLVIEW_AXIS_Y, 768 ,800)];
    else
        self.SVw_Scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SCROLLVIEW_AXIS_Y, SCREENWIDTH, SCROLLHEIGHT)];
    
    SVw_Scroll.contentSize = CGSizeMake(SCREENWIDTH, SCROLLHEIGHT);
    
    [self.view addSubview:SVw_Scroll];
    
    [self fetchData];
    
    [Bt_EditClippings addTarget:self
                       action:@selector(SelectClippingPressed:)
             forControlEvents:UIControlEventTouchUpInside];

}

- (void)SetMediaLocation:(MediaListsMedia *)Media
                   index:(int)index
{
    [self CheckOverLapping:Media index:index];
    /**int i = 0;
    
    while (i >= 0)
    {
        i = [self CheckOverLapping:Media index:index];
        
        //End of SetMediaLocation.
        if(i >= [AB_DetailMyboard.MediaArray count])
            i = -1;
    }**/
}

- (int)CheckOverLapping:(MediaListsMedia *)Media index:(int)index
{
    /**int i = 0;
    
    int Check_m_Max_Y = 0;
    
    NSLog(@"text_title:%@\ntext_description:%@",Media.text.text_title,Media.text.text_description);

    
    if(Media.Width == 0 && Media.Height == 0)
        return -1;
    
    for( MediaListsMedia *existMedia in AB_DetailMyboard.MediaArray) {
        
        //NSLog(@"Width:%d\nHeight:%d",existMedia.Width,existMedia.Height);
        
        if(i < index) {
            
            //Check Media is or not overlapping.
            if( existMedia.x != 0 && (Media.x <= existMedia.x + existMedia.Width) && (Media.y <= existMedia.y + existMedia.Height)) {
                Media.x = existMedia.x + existMedia.Width + M_BUFFER_X;
                
                //Range out of Screen.
                if(Media.x + Media.Width + M_BUFFER_X > SCREENWIDTH) {
                    Media.x = M_BUFFER_X;
                    
                    if(m_Max_Y < (existMedia.y + existMedia.Height+ M_BUFFER_Y))
                        m_Max_Y = (existMedia.y + existMedia.Height+ M_BUFFER_Y);
                    
                    if (Check_m_Max_Y>m_Max_Y)
                        m_Max_Y = Check_m_Max_Y;
                    
                    Media.y = m_Max_Y;
                }
                
                if(Check_m_Max_Y < (existMedia.y + existMedia.Height+ M_BUFFER_Y))
                    Check_m_Max_Y = (existMedia.y + existMedia.Height+ M_BUFFER_Y);
            }
        }
        if (i<index) {
            
            if (((existMedia.x<=Media.x)&&(Media.x<=existMedia.x+existMedia.Width))||
                ((existMedia.x<=Media.x+Media.Width)&&(Media.x+Media.Width<=existMedia.x+existMedia.Width))) {
                
                Media.x = existMedia.x + existMedia.Width + M_BUFFER_X;
            }
            
            //Range out of Screen.
            if (Media.x + Media.Width + M_BUFFER_X > SCREENWIDTH)
            {
                Media.x = M_BUFFER_X;
                if(m_Max_Y < (existMedia.y + existMedia.Height+ M_BUFFER_Y))
                    m_Max_Y = (existMedia.y + existMedia.Height+ M_BUFFER_Y);
                
                Media.y = m_Max_Y;
            }
            else if (((existMedia.y<=Media.y)&&(Media.y<=existMedia.y+existMedia.Height))||
                    ((existMedia.y<=Media.y+Media.Height)&&(Media.y+Media.Height<=existMedia.y+existMedia.Height))) {
                
                Media.y = m_Max_Y;
            }
        }**/
    
    
    if(Media.Width == 0 && Media.Height == 0)
        return -1;
    
    if (index == 0)
    {
        Media.x = M_BUFFER_X;
        m_Max_Y = Media.Height + M_BUFFER_Y;
    }
    else {
        MediaListsMedia *Previous_Media = [AB_DetailMyboard.MediaArray objectAtIndex:index-1];
        
        if( Previous_Media.x != 0 && (Media.x <= Previous_Media.x + Previous_Media.Width) && (Media.y <= Previous_Media.y + Previous_Media.Height)) {
            
            Media.x = Previous_Media.x + Previous_Media.Width + M_BUFFER_X;
            
            if(m_Max_Y < (Previous_Media.y + Previous_Media.Height+ M_BUFFER_Y))
                m_Max_Y = (Previous_Media.y + Previous_Media.Height+ M_BUFFER_Y);
            
            //Range out of Screen.
            if (Media.x + Media.Width + M_BUFFER_X > SCREENWIDTH) {
                
                Media.x = M_BUFFER_X;
                Media.y = m_Max_Y;
            }
            else
                Media.y = Previous_Media.y;
        }
    }
    
        //i++;
    //}
    
    return -1;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    //Clean Cell View.
    [Vw_PhotoList removeFromSuperview];
    [Vw_EditClippings removeFromSuperview];
    [Vw_CreateBook removeFromSuperview];
    [Vw_SelectClipping removeFromSuperview];
    [Vw_QuestionView removeFromSuperview];
    SVw_Scroll.userInteractionEnabled = YES;
}

#pragma mark - Bottom Feature

-(IBAction)OpenMyBooks:(id)sender
{
    [self.delegate DetailMyBoardUIViewControllerDidClose:self];
}

-(IBAction)OpenStream:(id)sender {
    
    [self.delegate DetailMyBoardUIViewControllerDidStream:self];
}

-(IBAction)OpenAccount:(id)sender {
    
    [self.delegate DetailMyBoardUIViewControllerDidAccount:self];
}

-(IBAction)OpenFeedSet:(id)sender {
    
    [self.delegate DetailMyBoardUIViewControllerDidFeedSet:self];
}


-(IBAction)MyPhotoPressed:(id)sender {
    
    SVw_Scroll.userInteractionEnabled = NO;
    if (Vw_PhotoList == nil) {
        
        UIButton *Bt_Photo = (UIButton *)sender;
        
        Vw_PhotoList = [[[NSBundle mainBundle] loadNibNamed:@"SelectMyPhoto" owner:self options:nil] objectAtIndex:0];
        Vw_PhotoList.delegate = self;
        Vw_PhotoList.BK_En_object = self.BK_En_object;
        [Vw_PhotoList GetController:self UserInfo:Array_AccountInfo];
        Vw_PhotoList.frame = CGRectMake(Bt_Photo.frame.origin.x+Bt_Photo.frame.size.width/2-PHOTO_ARROW_AXIS_X,
                                        Bt_Photo.superview.frame.origin.y-Vw_PhotoList.frame.size.height,
                                        Vw_PhotoList.frame.size.width,
                                        Vw_PhotoList.frame.size.height);
    }
    
    [Vw_PhotoList setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_PhotoList setAlpha:1.0];[self.view addSubview:Vw_PhotoList];}
                     completion:^(BOOL finished){ }];
}

-(IBAction)QuestionPressed:(id)sender {
    
    if (Vw_QuestionView == nil) {
        
        UIButton *Bt_Question = (UIButton *)sender;
        
        Vw_QuestionView = [[[NSBundle mainBundle] loadNibNamed:@"QuestionView" owner:self options:nil] objectAtIndex:0];
        Vw_QuestionView.frame = CGRectMake(Bt_Question.center.x-QUESTION_ARROW_AXIS_X,
                                           Bt_Question.superview.frame.origin.y-Vw_QuestionView.frame.size.height,
                                           Vw_QuestionView.frame.size.width,
                                           Vw_QuestionView.frame.size.height);
        
        Vw_QuestionView.delegate = self;
        [Vw_QuestionView Initialize];
    }
    
    [Vw_QuestionView setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_QuestionView setAlpha:1.0];[self.view addSubview:Vw_QuestionView];}
                     completion:^(BOOL finished){ }];
}

#pragma mark - Interface Orientation Management

//For iOS 5
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

//For iOS 6
-(BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

#pragma mark - Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if([[segue identifier] isEqualToString:@"Detail_Segue"]) {
        DetailViewController *VC_Detail = segue.destinationViewController;
        VC_Detail.MLM_detail = [AB_DetailMyboard.MediaArray objectAtIndex:[((UIButton *)sender).superview tag]-MEDIA_TAG];
        VC_Detail.BK_En_object = self.BK_En_object;
        VC_Detail.delegate = self;
    }
    else if([[segue identifier] isEqualToString:@"Stream_Segue"]){
        
        HomePageViewController *VC_Stream = segue.destinationViewController;
        VC_Stream.Array_AccountInfo = Array_AccountInfo;
    }
}

-(void)MediaPressed:(id)sender {
    
    [Bt_EditClippings removeTarget:self
                            action:@selector(EditClippingPressed:)
                  forControlEvents:UIControlEventTouchUpInside];
    
    [Bt_EditClippings addTarget:self
                         action:@selector(editclippingsClose:)
               forControlEvents:UIControlEventTouchUpInside];
    int CellIndex = [((UIButton *)sender).superview tag];
    
    //Open edit style
    if (edit==TRUE) {
        
        //Check media status.
        if ([Array_MediaSelected objectAtIndex:CellIndex-MEDIA_TAG]!=[self.AB_DetailMyboard.MediaArray objectAtIndex:CellIndex-MEDIA_TAG]) {
            
            [[SVw_Scroll viewWithTag:CellIndex].layer setMasksToBounds:YES];
            [[SVw_Scroll viewWithTag:CellIndex].layer setCornerRadius:10.0];
            [[SVw_Scroll viewWithTag:CellIndex].layer setBorderWidth:4.0];
            [[SVw_Scroll viewWithTag:CellIndex].layer setBorderColor:[UIColor colorWithRed:137.0/255.0 green:211.0/255.0 blue:223.0/255.0 alpha:1.0].CGColor];
            
            [Array_MediaSelected replaceObjectAtIndex:CellIndex-MEDIA_TAG withObject:[self.AB_DetailMyboard.MediaArray objectAtIndex:CellIndex-MEDIA_TAG]];
        }
        else {
            [[SVw_Scroll viewWithTag:CellIndex].layer setMasksToBounds:NO];
            [[SVw_Scroll viewWithTag:CellIndex].layer setCornerRadius:0.0];
            [[SVw_Scroll viewWithTag:CellIndex].layer setBorderWidth:0.0];
            [Array_MediaSelected replaceObjectAtIndex:CellIndex-MEDIA_TAG withObject:@"NonSelect"];
        }
        
        for (UIView *Temp_Cell in SVw_Scroll.subviews) {
            if (Temp_Cell.layer.masksToBounds==YES)
            {
                [Bt_EditClippings removeTarget:self
                                        action:@selector(editclippingsClose:)
                              forControlEvents:UIControlEventTouchUpInside];
                
                [Bt_EditClippings addTarget:self
                                           action:@selector(EditClippingPressed:)
                                 forControlEvents:UIControlEventTouchUpInside];
            }
        }
    }
    //Close edit style
    else {
        [self performSegueWithIdentifier:@"Detail_Segue" sender:sender];
    }
}


#pragma mark - Clippings

-(void)editclippingsMode:(id)sender
{
    
    [Bt_EditClippings removeTarget:self
                            action:@selector(editclippingsMode:)
                  forControlEvents:UIControlEventTouchUpInside];
    
    [Bt_EditClippings addTarget:self
                         action:@selector(editclippingsClose:)
               forControlEvents:UIControlEventTouchUpInside];
}
-(void)SelectClippingPressed:(id)sender
{
    
    [Bt_EditClippings setImage:[UIImage imageNamed:@"Done.png"] forState:UIControlStateNormal];
    SVw_Scroll.userInteractionEnabled = NO;
    
    if (Vw_SelectClipping==nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"SelectClipping" owner:self options:nil];
        Vw_SelectClipping.frame = CGRectMake(self.view.bounds.size.width/2-self.Vw_SelectClipping.frame.size.width/2,
                                            self.view.bounds.size.height/2-self.Vw_SelectClipping.frame.size.height/2,
                                            Vw_SelectClipping.frame.size.width,
                                            Vw_SelectClipping.frame.size.height);
    }
    [Vw_SelectClipping setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_SelectClipping setAlpha:1.0];[self.view addSubview:Vw_SelectClipping];}
                     completion:^(BOOL finished){ }];
    
}


-(void)EditClippingPressed:(id)sender {
    
    if (Vw_EditClippings==nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"EditClipping" owner:self options:nil];
        Vw_EditClippings.frame = CGRectMake(self.view.bounds.size.width/2-self.Vw_EditClippings.frame.size.width/2,
                                            self.view.bounds.size.height/2-self.Vw_EditClippings.frame.size.height/2,
                                            Vw_EditClippings.frame.size.width,
                                            Vw_EditClippings.frame.size.height);
        
        Vw_EditClippings.AMB_MyBoard = [[self.Array_AccountInfo objectAtIndex:0] MyBoard];
        Vw_EditClippings.Current_DetailMyBoard = self.AB_DetailMyboard;
        Vw_EditClippings.BK_En_object = self.BK_En_object;
        
        Vw_EditClippings.delegate = self;
    }
    [Vw_EditClippings initArray:[[[self.Array_AccountInfo objectAtIndex:0] MyBoard] BoardArray]];
    Vw_EditClippings.Array_MediaSelected = self.Array_MediaSelected;
    [Vw_EditClippings setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_EditClippings setAlpha:1.0];[self.view addSubview:Vw_EditClippings];}
                     completion:^(BOOL finished){ }];
}

-(IBAction)SelectClippingDidOk:(id)sender {
    
    edit = TRUE;
    SVw_Scroll.userInteractionEnabled = YES;
    [self.Vw_SelectClipping removeFromSuperview];
    
    [Bt_EditClippings removeTarget:self
                            action:@selector(SelectClippingPressed:)
                  forControlEvents:UIControlEventTouchUpInside];
    
    [Bt_EditClippings addTarget:self
                         action:@selector(editclippingsMode:)
               forControlEvents:UIControlEventTouchUpInside];
}

- (void)removeEditClipping {
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_EditClippings setAlpha:0.0];}
                     completion:^(BOOL finished){ [Vw_EditClippings removeFromSuperview]; }];
    Vw_EditClippings =nil;
}

-(void)editclippingsClose:(id)sender {
    
    edit = FALSE;
    
    [Bt_EditClippings setImage:[UIImage imageNamed:@"edit_clipping.png"] forState:UIControlStateNormal];
    
    [Bt_EditClippings removeTarget:self
                            action:@selector(editclippingsClose:)
                  forControlEvents:UIControlEventTouchUpInside];
    
    [Bt_EditClippings addTarget:self
                         action:@selector(SelectClippingPressed:)
               forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - FullScreen

-(IBAction)FullScreenOn:(id)sender {
    
    //Create FullScreenOff Button.
    Bt_FullScreenOff = [[UIButton alloc] initWithFrame:CGRectMake(955, 6, 54, 37)];
    [Bt_FullScreenOff setImage:[UIImage imageNamed:@"exit_full_screen.png"] forState:UIControlStateNormal];
    [Bt_FullScreenOff setAlpha:0];
    
    //Create Title Label.
    Lab_title = [[UILabel alloc] initWithFrame:CGRectMake(450, 7, 177, 28)];
    [Lab_title setFont:[UIFont systemFontOfSize:24]];
    Lab_title.backgroundColor = [UIColor blackColor];
    Lab_title.textColor = [UIColor grayColor];
    Lab_title.text = AB_DetailMyboard.name;
    [Lab_title setAlpha:0];
    
    //Create Line ImageView.
    IVw_dottedline = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 1024, 21)];
    [IVw_dottedline setContentMode:UIViewContentModeCenter];
    [IVw_dottedline setImage:[UIImage imageNamed:@"dottedline.png"]];
    [IVw_dottedline setAlpha:0];
    
    //Animation
    [UIView animateWithDuration:0.5
                     animations:^{
                         [self.view bringSubviewToFront:IVw_Background];
                         IVw_Background.image = nil;
                         IVw_Background.backgroundColor = [UIColor blackColor];
                         SVw_Scroll.frame = CGRectMake(0, SCROLLVIEW_FULLSCREEN_AXIS_Y, SCREENWIDTH, SCREENHEIGHT-SCROLLVIEW_FULLSCREEN_AXIS_Y);
                     }
                     completion:^(BOOL finished){
                         [self.view bringSubviewToFront:SVw_Scroll];
                         [Bt_FullScreenOff setAlpha:1.0];
                         [self.view addSubview:Bt_FullScreenOff];
                         [Lab_title setAlpha:1.0];
                         [self.view addSubview:Lab_title];
                         [IVw_dottedline setAlpha:1.0];
                         [self.view addSubview:IVw_dottedline];
                         [self FullScreenPressed:TRUE];
                     }];
    
    [Bt_FullScreenOff addTarget:self action:@selector(FullScreenOff:) forControlEvents:UIControlEventTouchUpInside];
    [Bt_FullScreenOff setImage:[UIImage imageNamed:@"fullscreen_off.png"] forState:(UIControlState)UIControlEventTouchUpInside];
}

-(void)FullScreenOff:(id)sender {
    
    [UIView animateWithDuration:0.5
                     animations:^{
                         [Bt_FullScreenOff removeFromSuperview];
                         [Lab_title removeFromSuperview];
                         [IVw_dottedline removeFromSuperview];
                         SVw_Scroll.frame = CGRectMake(0, SCROLLVIEW_AXIS_Y, SCREENWIDTH,SCROLLHEIGHT);
                         [self FullScreenPressed:FALSE];
                     }
                     completion:^(BOOL finished){
                         IVw_Background.image = [UIImage imageNamed:@"bg_2.png"];
                         IVw_Background.backgroundColor = [UIColor clearColor];
                         [self.view insertSubview:IVw_Background atIndex:0];
                     }];
    
}


#pragma mark - SelectMyPhotoDelegate

-(void) uiViewTouched_SelectMyPhoto:(BOOL)wasInSide {
    if(!wasInSide) {
        [Vw_PhotoList removeFromSuperview];
        self.SVw_Scroll.userInteractionEnabled = YES;
    }
}

- (void)PhotoSaveSuccess:(NSString *)BoardName {
    [Vw_PhotoList removeFromSuperview];
    self.SVw_Scroll.userInteractionEnabled = YES;
    
    NSString *NS_Message = [NSString stringWithFormat:@"Great! This photo has been clipped to your \"%@\" Books",BoardName];
    UIAlertView *AlertView = [[UIAlertView alloc]initWithTitle:@"Success" message:NS_Message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    
    [AlertView show];
}

#pragma mark - QuestionViewDelegate

- (void)uiViewTouched_Question:(BOOL)wasInSide {
    
    if(!wasInSide)
        [Vw_QuestionView removeFromSuperview];
}

#pragma mark - MediaLocation Function.

-(void)MediaLocationPressed {
    
    int Temp_Content_Y = 0;
    
    int MediaIndex = 0;
    
    //MediaListsMedia *MLM_Media = [[MediaListsMedia alloc] init];
    Array_MediaSelected = [[NSMutableArray alloc] init];
    
    MediaListsMedia *MLM_Media = [[MediaListsMedia alloc] init];
    
    //Init x and y of all media.
    for (MLM_Media in self.AB_DetailMyboard.MediaArray)
        MLM_Media.x=MLM_Media.y=0;
    
    for (MLM_Media in self.AB_DetailMyboard.MediaArray) {
        
        DetailMyBoardCell *Buffer_Cell = (DetailMyBoardCell *)[self.SVw_Scroll viewWithTag:MediaIndex+MEDIA_TAG];
        
        [Bt_Media addTarget:self action:@selector(MediaPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        if(self.interfaceOrientation == UIInterfaceOrientationPortrait)
            [Buffer_Cell updateDetailCellInfo:MLM_Media
                                     MaxWidth:self.view.frame.size.width
                                   FullScreen:FALSE];
        else
            [Buffer_Cell updateDetailCellInfo:MLM_Media
                                     MaxWidth:self.view.frame.size.height
                                   FullScreen:FALSE];
        
        //Dynamically Set the XY of the Media Location
        [self SetMediaLocation:MLM_Media index:MediaIndex];
        [UIView animateWithDuration:1
         animations:^{
         Buffer_Cell.frame =CGRectMake(MLM_Media.x, MLM_Media.y, MLM_Media.Width, MLM_Media.Height);
         }
         completion:nil];
         
         if (Temp_Content_Y<(MLM_Media.y + MLM_Media.Height + M_BUFFER_Y))
         Temp_Content_Y = MLM_Media.y + MLM_Media.Height + M_BUFFER_Y;
         
         [Array_MediaSelected addObject:@"NonSelect"];
        
        MediaIndex++;
    }
    
    SVw_Scroll.contentSize = CGSizeMake(SCREENWIDTH , Temp_Content_Y);
}

#pragma mark - FullScreen Function.

-(void)FullScreenPressed:(BOOL)SetFullScreen {
    
    int MediaIndex = 0;
    
    for (MediaListsMedia *MLM_Media in self.AB_DetailMyboard.MediaArray) {
        
        DetailMyBoardCell *Buffer_Cell = (DetailMyBoardCell *)[self.SVw_Scroll viewWithTag:MediaIndex+MEDIA_TAG];
        
        [Bt_Media addTarget:self action:@selector(MediaPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        if(self.interfaceOrientation == UIInterfaceOrientationPortrait)
            [Buffer_Cell updateDetailCellInfo:MLM_Media
                                     MaxWidth:self.view.frame.size.width
                                   FullScreen:SetFullScreen];
        else
            [Buffer_Cell updateDetailCellInfo:MLM_Media
                                     MaxWidth:self.view.frame.size.height
                                   FullScreen:SetFullScreen];
        
        MediaIndex++;
    }
}

#pragma mark - DetailViewcontrollerDelegate

-(void)DetailViewControllerDidClose:(DetailViewController *)controller
{
    [self dismissViewControllerAnimated:NO completion:nil];
    
    [Bt_EditClippings removeTarget:self
                            action:@selector(editclippingsClose:)
                  forControlEvents:UIControlEventTouchUpInside];
    
    [Bt_EditClippings addTarget:self
                         action:@selector(SelectClippingPressed:)
               forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - EditClippingDelegate

-(void) DidSelected_EditClipping:(NSMutableArray *)SelectedMediaArray SetAction:(NSString *)Action {
    
    [self editclippingsClose:self];
    
    self.Array_MediaSelected = [[NSMutableArray alloc] init];
    
    if (![Action isEqualToString:@"Copy"]) {
        
        
        //Init Tag.
        for (DetailMyBoardCell *Current_Cell in SVw_Scroll.subviews) {
            
            if (Current_Cell.clipsToBounds==YES)
                [Current_Cell removeFromSuperview];
        }
        
        int Index = 0;
        
        //Init Tag.
        for (DetailMyBoardCell *Current_Cell in SVw_Scroll.subviews) {
            
            if ([Current_Cell isKindOfClass:[DetailMyBoardCell class]]) {
                
                Current_Cell.tag = Index+MEDIA_TAG;
                
                [[SVw_Scroll viewWithTag:Current_Cell.tag].layer setMasksToBounds:NO];
                [[SVw_Scroll viewWithTag:Current_Cell.tag].layer setCornerRadius:0.0];
                [[SVw_Scroll viewWithTag:Current_Cell.tag].layer setBorderWidth:0.0];
                Index++;
            }
        }
        
        [self MediaLocationPressed];
    }
    else {
        
        //Init Cells.
        for (DetailMyBoardCell *Current_Cell in SVw_Scroll.subviews) {
            
            if ([Current_Cell isKindOfClass:[DetailMyBoardCell class]]) {
                
                [Current_Cell.layer setMasksToBounds:NO];
                [Current_Cell.layer setCornerRadius:0.0];
                [Current_Cell.layer setBorderWidth:0.0];
                [Array_MediaSelected addObject:@"NonSelect"];
            }
        }
    }
}

-(void) CreatedNewBookPressed {
    
    [Vw_EditClippings removeFromSuperview];
    
    if (Vw_CreateBook == nil) {
        Vw_CreateBook = [[[NSBundle mainBundle] loadNibNamed:@"CreateBookView" owner:self options:nil] objectAtIndex:0];
        Vw_CreateBook.frame = CGRectMake(self.view.bounds.size.width/2-self.Vw_CreateBook.frame.size.width/2,
                                         self.view.bounds.size.height/2-self.Vw_CreateBook.frame.size.height/2,
                                         self.Vw_CreateBook.frame.size.width,
                                         self.Vw_CreateBook.frame.size.height);
        
        Vw_CreateBook.BK_En_object = self.BK_En_object;
        
        Vw_CreateBook.delegate = self;
        Vw_CreateBook.Tf_BookName.delegate = Vw_CreateBook;
    }
    SVw_Scroll.userInteractionEnabled = NO;
    Vw_CreateBook.AMB_MyBoard = [[self.Array_AccountInfo objectAtIndex:0] MyBoard];

    [Vw_CreateBook setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_CreateBook setAlpha:1.0];[self.view addSubview:Vw_CreateBook];}
                     completion:^(BOOL finished){ }];
}

-(void)uiViewTouched_EditClipping:(BOOL)wasInSide {
    
    if(!wasInSide) {
        [Vw_EditClippings removeFromSuperview];
        SVw_Scroll.userInteractionEnabled = YES;
    }
}

#pragma mark - CreateBookViewDelegate

-(void)uiViewCancel {
    
    [Vw_CreateBook removeFromSuperview];
    self.SVw_Scroll.userInteractionEnabled = YES;
}

-(void) ViewDidCreated {
    
    [Vw_CreateBook removeFromSuperview];
    self.SVw_Scroll.userInteractionEnabled = YES;
    
    [self.Vw_EditClippings initArray:((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).MyBoard.BoardArray];
    self.Vw_EditClippings.Lab_BookName.text = ((AccountBoard *)[((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).MyBoard.BoardArray lastObject]).name;
    self.Vw_EditClippings.Bt_SelectBoard.tag = ((AccountBoard *)[((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).MyBoard.BoardArray lastObject]).BoardID;
    
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_EditClippings setAlpha:1.0];[self.view addSubview:Vw_EditClippings];}
                     completion:^(BOOL finished){ }];
}

@end
